//
//  ORCustomTableViewCell.m
//  NewsHots
//
//  Created by rimi on 2016/12/3.
//  Copyright © 2016年 zhongshuilian. All rights reserved.
//

#import "ORCustomTableViewCell.h"
#import "ORCollectionViewCell.h"
#import "ORRecommendViewController.h"


@interface ORCustomTableViewCell () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *nameLabels;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *realtype;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *author_name;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, assign) CGFloat imgHei;
@property (nonatomic, assign) CGFloat nameHei;
@property (nonatomic, strong) NSArray *reArray;

@end

@implementation ORCustomTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
        NSDictionary *dic = @{@"key":@"7926709a9c294a2b8fdada7f0cabe81e",@"rows":@(30),@"page":@(1)};
        
        [ORNetWorkRequestManager getMethod:@"http://api.avatardata.cn/WorldNews/Query" withParames:dic Success:^(id respondObject) {
            _reArray = respondObject[@"result"];
            
            [self.collectionView reloadData];
            
        } Failure:^(NSError *error) {
            
        }];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    if (self.isHidde == NO) {
        [self.realtype removeFromSuperview];
        [self.imgView removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.author_name removeFromSuperview];
        [self.contentView addSubview:self.nameLabels];
        [self.contentView addSubview:self.collectionView];
        
    }else if (self.isHidde == YES){
        [self.nameLabels removeFromSuperview];
        [self.collectionView removeFromSuperview];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.imgView];
        [self.contentView addSubview:self.realtype];
        [self.contentView addSubview:self.author_name];
    }
    
    NSString *text = self.titleLabel.text;
    CGSize rect = [ORCustomTableViewCell textSize:text];
    
    self.titleLabel.frame = AAdaptionRect(CGRectGetMaxX(self.imgView.frame) / AAdaptationWidth() + 10, CGRectGetMaxY(self.realtype.frame) / AAdaptationWidth() + 5, kBaseWidth - CGRectGetMaxX(self.imgView.frame)/AAdaptationWidth() - 30, (rect.height + 5) / AAdaptationWidth());
    

    CGRect timeRect = self.author_name.frame;
    timeRect.origin.y = CGRectGetMaxY(_titleLabel.frame);
    self.author_name.frame = timeRect;

}

+ (CGSize)textSize:(NSString *)text {
    
    CGRect rectText = [text boundingRectWithSize:CGSizeMake( AAdaptation(kBaseWidth - 100 - 30), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:AAFont(16)} context:nil];
    return rectText.size;
}

#pragma mark - UICollectionViewDelegate &UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 15;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.reArray.count == 0) {
        
        NSDictionary *dic = @{@"key":@"7926709a9c294a2b8fdada7f0cabe81e",@"rows":@(30),@"page":@(1)};
        [ORNetWorkRequestManager getMethod:@"http://api.avatardata.cn/WorldNews/Query" withParames:dic Success:^(id respondObject) {
            _reArray = respondObject[@"result"];
            [self.collectionView reloadData];
            
        } Failure:^(NSError *error) {
            
        }];

    }
    ORCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ORCollectionViewCell" forIndexPath:indexPath];
        cell.nameLabel.text = _reArray[indexPath.row][@"title"];
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:_reArray[indexPath.row][@"picUrl"]] placeholderImage:[UIImage imageNamed:@"4"]];
    NSLog(@"%@", cell.nameLabel.text);
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ORRecommendViewController *recommened = [[ORRecommendViewController alloc]init];
    recommened.url = _reArray[indexPath.row][@"url"];
    recommened.hidesBottomBarWhenPushed = YES;
    
    [[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject pushViewController:recommened animated:YES];
    recommened.hidesBottomBarWhenPushed = NO;
}

-(UILabel *)nameLabels{
    if (!_nameLabels) {
        _nameLabels = [[UILabel alloc]initWithFrame:AAdaptionRect(5, 5, 100, 18)];
        _nameLabels.font = AAFont(15);
        _nameLabels.text = @"实时推荐";
    }
    return _nameLabels;
}


-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = AAdaptationSize(110, 80);
        
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = AAdaptation(10);
        layout.minimumInteritemSpacing = AAdaptation((kBaseWidth - 110 * 3)/2);
        _collectionView = [[UICollectionView  alloc]initWithFrame:AAdaptionRect(10, CGRectGetMaxY(self.nameLabels.frame)/AAdaptationWidth()+5, kBaseWidth-20, 80) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ORCollectionViewCell class] forCellWithReuseIdentifier:@"ORCollectionViewCell"];
    }
    return _collectionView;
}

-(UIView *)bgView{
    if (!_bgView) {
        self.bgView = [[UIView alloc]initWithFrame:AAdaptionRect(10, 5, kBaseWidth- 20, 180)];
        self.bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

-(UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [[UIImageView alloc]initWithFrame:AAdaptionRect(10, 10, 100, 100)];
        // 按图片宽高比例来缩放
//        _imgView.contentMode = UIViewContentModeScaleAspectFit;
        [_imgView sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"4"]];
    }
    return _imgView;
}

-(UILabel *)realtype{
    if (!_realtype) {
        _realtype = [[UILabel alloc]initWithFrame:AAdaptionRect(CGRectGetMaxX(self.imgView.frame)/AAdaptationWidth()+10, 15, 200, 20)];
        _realtype.font = AAFont(15);
        _realtype.textColor = [UIColor darkGrayColor];
        
    }
    return _realtype;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:AAdaptionRect(CGRectGetMaxX(self.imgView.frame)/AAdaptationWidth()+10, CGRectGetMaxY(self.realtype.frame)/AAdaptationWidth()+5, kBaseWidth - CGRectGetMaxX(self.imgView.frame) - 30, 0)];
        _titleLabel.numberOfLines = 0;
        _titleLabel.font = AAFont(16);
    }
    return _titleLabel;
}

-(UILabel *)author_name{
    if (!_author_name) {
        _author_name = [[UILabel alloc]initWithFrame:AAdaptionRect(CGRectGetMaxX(self.imgView.frame)/AAdaptationWidth()+10, 0, 200, 20)];
        _author_name.font = AAFont(15);
        _author_name.textColor = MainColor;
    }
    return _author_name;
}


@end
